﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using INTEX.DataBase;
using INTEXTECH;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using INTEX.DataBase.Tools;
using ReflectionStudio.Controls;
namespace WPFExcelReport
{
    public partial class YXCAD2ITX : DocumentBase
    {
        #region====================Properties=======================
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        DataSet setCADMap = null;
        bool fail = false;
        DataRow LastRow = null;
        string failreason = "";
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData"); ;
            }
        }

        bool IsAutoRun = true;
        bool IsRunning = false;
        GB2312Convert8859P1 gb2312;
        public SETINTEXDB Set4ITX
        {
            get
            {
                return (SETINTEXDB)TryFindResource("Set4ITX");
            }
        }
        CollectionViewSource vwLogInfo
        {
            get
            {
                return (CollectionViewSource)this.FindResource("vwLogInfo");
            }
        }
        DataRow dwLogInfo
        {
            get
            {
                if (vwLogInfo.View == null)
                    return null;
                if ((DataRowView)vwLogInfo.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwLogInfo.View.CurrentItem).Row;
            }
        }
        YXCAD2ITXData m_Data;
        DataSet setDISPO = new DataSet();

        private string FClothNO
        {
            get
            {
                return txtCADART.Text;
            }
            set
            {
                txtCADART.Text = value;
            }
        }
        private string FInterID
        {
            get
            {
                return txtFInterID.Text;
            }
            set
            {
                txtFInterID.Text = value;
            }
        }
        public YXCAD2ITX()
        {
            try
            {
                InitializeComponent();
                this.Name = "YXCAD2ITX"; 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion
        public BackgroundWorker bgWorker;
 

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                FInterID = "1";
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    ReloadCon();
                }

                FClothNO = "J4-000";
                bgWorker = new BackgroundWorker();
                bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
                bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            EventDispatcher.Instance.RaiseStatus("停止自动转换", StatusEventType.StopProgress);
        }
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        { 
            EventDispatcher.Instance.RaiseStatus("开始自动转换", StatusEventType.StopProgress); 
            m_Data.AutoRun();
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    m_Data = new YXCAD2ITXData();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                IsSetUp = true;
            }
        }
        private void btnLogInfor_Click(object sender, RoutedEventArgs e)
        {
            LoadJob(FInterID, FClothNO);
        }


        private void btnAutoRun_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "正在转换"))
            {
                m_Data.IsAutoRun = true;
                m_Data.SetData = SetData;
                m_Data.SetDISPO = setDISPO;
                m_Data.SetCAD = setCADMap;
                bgWorker.RunWorkerAsync();
            }
        }


        public void ReloadCon()
        {

            DataSet setResult = new DataSet();
            setCADMap = new DataSet();
            setDISPO = m_Data.LoadSetDispo("");
            setCADMap.Merge(setDISPO);
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\YXCADMAPINTEX.XML");
            setResult.ReadXml(filePath);
            setCADMap.Merge(setResult);

            //setCADMap.Merge(setResult, false, MissingSchemaAction.Ignore);
            DataSet setTemp = new DataSet();
            filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\FINISHMAPINTEX.XML");
            setTemp.ReadXml(filePath);
            setCADMap.Merge(setTemp);
            setCADMap.Tables["FINISHMAP"].Clear();
            setResult = m_Data.QueryFINISHINGTECHMASTER();
            foreach (DataRow rf in setResult.Tables[0].Rows)
            {
                bool exist = false;
                var query = from DataRow rfc in setCADMap.Tables["FINISHMAP"].AsEnumerable()
                            where rfc["ITXFINISH"].ToString() == rf["FShortCode"].ToString()
                            select rfc;
                foreach (DataRow rq in query)
                {
                    exist = true;
                    rq["CADFINISH"] = rf["FFinishingTechName"];
                    rq["DESCRIPTION"] = rf["FFinishingTechName"];
                    rq["PROCESSFLOW"] = rf["FWholeFinishingName"];
                }
                if (!exist)
                {

                    DataRow rn = setCADMap.Tables["FINISHMAP"].NewRow();
                    rn["ROWID"] = "x";
                    rn["ITXFINISH"] = rf["FShortCode"];
                    rn["CADFINISH"] = rf["FFinishingTechName"];
                    rn["DESCRIPTION"] = rf["FFinishingTechName"];
                    rn["PFID"] = rf["FShortCode"];
                    rn["PROCESSFLOW"] = rf["FWholeFinishingName"];
                    setCADMap.Tables["FINISHMAP"].Rows.Add(rn);
                    //rn["REMARK"] = rf["FWholeFinishingName"];
                }
            }
            m_Data.SetCAD = setCADMap;
            m_Data.SetDISPO = setDISPO;
        }
        public void LoadJob(string finterid, string FProductNO = "")
        {
            try
            {
                SetData.Clear();
                DataSet setResult = m_Data.QueryLogInfor(finterid, FProductNO);
                SetData.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnStopRun_Click(object sender, RoutedEventArgs e)
        {
            IsRun.Text = "N";
            m_Data.IsAutoRun = false;
        }
    }
}